Listing 10 - Page 0: No Title

##############################################################################
# Python From Scratch
# Autor: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2024
# Site: https://pythonfromscratch.com
#
# File: listing\chapter 10\10.1443 - No Title.py
# Description: No Title
##############################################################################

class UniqueList:
    def  __init__(self, elem_class):
        self.list = []
        self.elem_class = elem_class
    def __len__(self):
        return len(self.list)
    def __iter__(self):
        return iter(self.list)
    def __getitem__(self, position):
        return self.list[position]
    def valid_index(self, index):
        return index >= 0 and index < len(self.list)
    def add(self, elem):
        if self.search(elem) == -1:
            self.list.append(elem)
    def remove(self, elem):
        self.list.remove(elem)
    def search(self, elem):
        self.verify_type(elem)
        try:
            return self.list.index(elem)
        except ValueError:
            return -1
    def verify_type(self, elem):
        if not isinstance(elem, self.elem_class):
            raise TypeError("Invalid type")
    def sort(self, key=None):
        self.list.sort(key=key)
Click here to download the file